package business.global.pk.tdk;

import java.util.ArrayList;
import java.util.List;

import business.tdk.c2s.iclass.STDK_PosShangFen;
import cenum.RoomTypeEnum;
import com.ddm.server.common.utils.CommTime;
import com.ddm.server.websocket.def.ErrorCode;
import com.ddm.server.websocket.handler.requset.WebSocketRequest;


public class TDKRoundPos {
    protected TDKSetRound round;
    protected TDKRoomSet set;
    protected TDKSetPos pos;

    protected int opPos; // 当前等待操作的pos
    protected boolean isFlash = false;

    protected final int FlashSec = 3; // ms
    protected final int FastOutSec = 5;// s

    // 可接受的操作
    protected List<TDKdefine.TDK_OP> recieveOpTypes = new ArrayList<>();
    protected boolean publicWait = true; // 公开在操作，别人可以看到读条（出牌行为是公开的）

    protected TDKdefine.TDK_OP opType; // 最终执行的操作
    protected int opCard; // 操作的牌

    protected  TDKdefine.TDK_OP aliasOpType;// GenZhu,Ti,FanTi,YaZhu

    public TDKdefine.TDK_OP getAliasOpType() {
        return aliasOpType;
    }

    public void setAliasOpType(TDKdefine.TDK_OP aliasOpType) {
        this.aliasOpType = aliasOpType;
    }

    public void addOpType(List<TDKdefine.TDK_OP> recieveOpTypes) {
        if (null == recieveOpTypes) {
            return;
        }
        this.recieveOpTypes.addAll(recieveOpTypes);
    }

    public void addOpType(TDKdefine.TDK_OP opType) {
        if (null == opType) {
            return;
        }
        this.recieveOpTypes.add(opType);
    }

    public int sizeRecieveOpTypes() {
        return this.recieveOpTypes.size();
    }

    public List<TDKdefine.TDK_OP> getRecieveOpTypes() {
        return this.recieveOpTypes;
    }

    public boolean checkRecieveOpTypes(TDKdefine.TDK_OP opType) {
        return getRecieveOpTypes().contains(opType);
    }


    public boolean isPublicWait() {
        return publicWait;
    }

    public void tryEndRound() {
        this.round.tryEndRound();
    }


    /**
     * 操作错误
     *
     * @param request
     * @param opType
     * @return
     */
    public int errorOpType(WebSocketRequest request, TDKdefine.TDK_OP opType) {
        // 操作错误
        if (!checkRecieveOpTypes(opType)) {
            request.error(ErrorCode.NotAllow, "errorOpType : " + opType);
            return -1;
        }
        return 1;
    }


    public TDKRoundPos(TDKSetRound round, int opPos) {
        this.round = round;
        this.opPos = opPos;
        this.set = round.getSet();
        this.pos = this.set.posDict.get(opPos);
    }


    /**
     * 弃权
     *
     * @param request
     * @param opType
     * @return
     */
    public int op_QiPai(WebSocketRequest request, TDKdefine.TDK_OP opType) {
        // 操作错误
        if (errorOpType(request, opType) <= 0) {
            return -1;
        }
        this.pos.setQiPai(true);
        this.set.setAllQiPai(this.set.getSetPosMgr().checkAllQiPai());
        this.round.setSetHuEnd(this.set.isAllQiPai());
        // 设置操作类型
        this.setOpType(opType);
        return 0;
    }

    /**
     * 押注
     *
     * @param request
     * @param opType
     * @return
     */
    public int op_YaZhu(WebSocketRequest request, TDKdefine.TDK_OP opType, int value) {
        // 操作错误
        if (errorOpType(request, opType) <= 0) {
            return -1;
        }
        // 检查押注值是否正确
        if (!TDKdefine.TDK_DIFEN_TYPE.checkValueOfSet(this.set.getRoom().getDiFen(), this.set.getCurChip(), value, this.set.getLanguoBeishu())) {
            request.error(ErrorCode.NotAllow, "yazhu value : {}", value);
            return -1;
        }
        //检查钱是不是够
        //System.out.println("RoomType:"+this.set.getRoom().getRoomTypeEnum()+", SportPoint:"+this.pos.getRoomPos().getRoomSportsPoint());
        if ( RoomTypeEnum.checkUnionOrClub(this.set.getRoom().getRoomTypeEnum())) {
            //当局消耗了的筹码
            if(this.pos.getRoomPos().getRoomSportsPoint()-this.pos.getChip()<value){
                request.error(ErrorCode.NotAllow, "竞技点不够，需要：{},你只有：{}",this.set.getCurChip(), this.pos.getRoomPos().getRoomSportsPoint());
                if(this.set.getShangFenPos()==-1){
                    this.set.setShangFenPos(this.pos.getPosID());
                    this.set.setShangFenOpTime(CommTime.nowSecond());
                }
                this.set.getRoom().getRoomPosMgr().notify2All(STDK_PosShangFen.make(this.set.getRoom().getRoomID(),this.pos.getPosID(),this.pos.getPid(),this.set.getShangFenOpTime(),this.set.getRoom().getShangFenTime(),CommTime.nowSecond()));
                return -1;
            }
        }
        this.pos.addChip(value);
        this.set.setOpPosValue(this.pos.getPosID(), value);
        if(this.getAliasOpType().equals(TDKdefine.TDK_OP.FanTi)||this.getAliasOpType().equals(TDKdefine.TDK_OP.Ti)){
            //System.out.println("Pos:"+this.pos.getPosID()+", PlayerId:"+this.pos.getPid()+", OpType:"+this.getAliasOpType());
            this.pos.setCurTiOrBuTi(this.getAliasOpType());
        }
        // 设置操作类型
        this.setOpType(opType);
        return 0;
    }

    /**
     * 押注
     *
     * @param request
     * @param opType
     * @return
     */
    public int op_BuTi(WebSocketRequest request, TDKdefine.TDK_OP opType) {
        // 操作错误
        if (errorOpType(request, opType) <= 0) {
            return -1;
        }
        this.pos.setCurTiOrBuTi(TDKdefine.TDK_OP.BuTi);
        // 设置操作类型
        this.setOpType(opType);
        return 0;
    }
    /**
     * 跟注
     *
     * @param request
     * @param opType
     * @return
     */
    public int op_GenZhu(WebSocketRequest request, TDKdefine.TDK_OP opType) {
        // 操作错误
        if (errorOpType(request, opType) <= 0) {
            return -1;
        }
        //检查钱是不是够
        //System.out.println("RoomType:"+this.set.getRoom().getRoomTypeEnum()+", SportPoint:"+this.pos.getRoomPos().getRoomSportsPoint());
        if ( RoomTypeEnum.checkUnionOrClub(this.set.getRoom().getRoomTypeEnum())) {
            if(this.pos.getRoomPos().getRoomSportsPoint()-this.pos.getChip()<this.set.getCurChip()) {
                request.error(ErrorCode.NotAllow, "竞技点不够，需要：{},你只有：{}", this.set.getCurChip(), this.pos.getRoomPos().getRoomSportsPoint());
                if(this.set.getShangFenPos()==-1){
                    this.set.setShangFenPos(this.pos.getPosID());
                    this.set.setShangFenOpTime(CommTime.nowSecond());
                }
                this.set.getRoom().getRoomPosMgr().notify2All(STDK_PosShangFen.make(this.set.getRoom().getRoomID(), this.pos.getPosID(), this.pos.getPid(),this.set.getShangFenOpTime(),this.set.getRoom().getShangFenTime(),CommTime.nowSecond()));
                return -1;
            }
        }

        this.pos.addChip(this.set.getCurChip());
        this.set.genZhu();
        // 设置操作类型
        this.setOpType(opType);
        return 0;
    }

    /**
     * 开牌
     *
     * @param request
     * @param opType
     * @return
     */
    public int op_KaiPai(WebSocketRequest request, TDKdefine.TDK_OP opType) {
        // 操作错误
        if (errorOpType(request, opType) <= 0) {
            return -1;
        }
        this.round.setSetHuEnd(true);
        // 设置操作类型
        this.setOpType(opType);
        return 0;
    }


    /**
     * 设置操作类型
     *
     * @param oPlayer
     */
    public void setOpType(TDKdefine.TDK_OP oPlayer) {
        //ysjvb  只要有所有操作就标记不是上分状态
        this.set.setShangFenPos(-1);
        // 设置回合状态
        this.round.setOpType(oPlayer);
        // 设置操作状态
        this.opType = oPlayer;
        this.tryEndRound();
    }


    public int getOpPos() {
        return opPos;
    }


    // 暂未实现 TODO
    public int op_Default() {
        this.tryEndRound();
        return 0;
    }

    public TDKdefine.TDK_OP getOpType() {
        return opType;
    }

    public int op(WebSocketRequest request, TDKdefine.TDK_OP opType, int value) {
        int opCardRet = -1;
        if (this.getOpType() != null) {
            request.error(ErrorCode.NotAllow, "opPos has opered");
            return opCardRet;
        }
        switch (opType) {
            case QiPai:
                opCardRet = op_QiPai(request, opType);
                break;
            case YaZhu:
                opCardRet = op_YaZhu(request, opType, value);
                break;
            case GenZhu:
                opCardRet = op_GenZhu(request, opType);
                break;
            case KaiPai:
                opCardRet = op_KaiPai(request, opType);
                break;
            case BuTi:
                opCardRet = op_BuTi(request, opType);
                break;
            default:
                break;
        }
        request.response();
        return opCardRet;
    }
}
